#include "my_log.h"
#include "stm32f1xx_hal.h"
#include "usart.h"

//extern UART_HandleTypeDef huart1;

#define USARTx_INSTANCE USART1

void my_log_output(const char *str, unsigned int len)
{
    //HAL_UART_Transmit(&huart1, (uint8_t *)str, len, 500);
    //LL_USART_TransmitData8(USART1, str[0]);

    uint16_t ubSend = 0;
    
    while(ubSend < len)
    {
        /* Wait for TXE flag to be raised */
        while(!LL_USART_IsActiveFlag_TXE(USARTx_INSTANCE))
        {
        }

        /* If last char to be sent, clear TC flag */
        if(ubSend == (len - 1))
        {
            LL_USART_ClearFlag_TC(USARTx_INSTANCE);
        }

        /* Write character in Transmit Data register.
           TXE flag is cleared by writing data in DR register */
        LL_USART_TransmitData8(USARTx_INSTANCE, str[ubSend++]);
    }

}


void my_log_init(void)
{
    /* close printf buffer */
    // setbuf(stdout, NULL);
    /* initialize EasyLogger */
    elog_init();
    /* set EasyLogger log format */
    elog_set_fmt(ELOG_LVL_ASSERT, ELOG_FMT_ALL);
    elog_set_fmt(ELOG_LVL_ERROR, ELOG_FMT_LVL | ELOG_FMT_TAG);
    elog_set_fmt(ELOG_LVL_WARN, ELOG_FMT_LVL | ELOG_FMT_TAG);
    elog_set_fmt(ELOG_LVL_INFO, ELOG_FMT_LVL | ELOG_FMT_TAG);
    elog_set_fmt(ELOG_LVL_DEBUG, ELOG_FMT_ALL & ~ELOG_FMT_FUNC);
    elog_set_fmt(ELOG_LVL_VERBOSE, ELOG_FMT_ALL & ~ELOG_FMT_FUNC);
    /* start EasyLogger */
    elog_start();
}
